namespace SimpleWebServices.Web.UI.Html.Validation
{
    using System;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class StandardSelectValidatorFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<StandardSelectValidator>()
                .DerivesFrom<Validator>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new StandardSelectValidator());
        }

        [Fact]
        public void ctor_IInnerElements()
        {
            Assert.NotNull(new StandardSelectValidator(new Label()));
        }

        [Fact]
        public void ctor_IInnerElementsNull()
        {
            Assert.NotNull(new StandardSelectValidator(null as IInnerElements));
        }

        [Fact]
        public void op_Configure_Form_DataCollection()
        {
            DataCollection data = new DataCollection();
            data.Add("Display", "display");
            data.Add("ErrorText", "error");

            Label label = new Label { Id = "display" };
            Form form = new Form
            {
                Elements = { label }
            };

            StandardSelectValidator obj = new StandardSelectValidator(null);
            obj.Configure(form, data);

            Assert.Same(label, obj.Display);
            Assert.Equal<string>("error", obj.ErrorText);
        }

        [Fact]
        public void op_Configure_Form_DataCollectionEmpty()
        {
            StandardSelectValidator obj = new StandardSelectValidator(null);
            obj.Configure(new Form(), new DataCollection());

            Assert.Null(obj.Display);
        }

        [Fact]
        public void op_Configure_Form_DataCollectionNull()
        {
            StandardSelectValidator obj = new StandardSelectValidator(null);
            obj.Configure(new Form(), null as DataCollection);

            Assert.Null(obj.Display);
        }

        [Fact]
        public void op_Validate_IElementNotIControl()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new StandardSelectValidator().Validate(new Anchor()));
        }

        [Fact]
        public void op_Validate_IElementNotSelect()
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => new StandardSelectValidator().Validate(new TextInput()));
        }

        [Fact]
        public void op_Validate_IElementNull()
        {
            Assert.Throws<ArgumentNullException>(() => new StandardSelectValidator().Validate(null as IElement));
        }

        [Fact]
        public void op_Validate_IElementValid()
        {
            string xml =
                "<select id='id' name='id'>" +
                    "<option value='value1'>text1</option>" +
                    "<optgroup label='label' >" +
                        "<option value='value2.1'>text2.1</option>" +
                        "<option value='value2.2'>text2.2</option>" +
                    "</optgroup>" +
                    "<option value='value3' selected='selected'>text3</option>" +
                "</select>";
            Select element = xml.Deserialize<Select>();

            StandardSelectValidator obj = new StandardSelectValidator(new Label());

            Assert.True(obj.Validate(element));

            Assert.Equal<int>(0, obj.Errors.Count);
        }

        [Fact]
        public void op_Validate_IElementInvalid()
        {
            string error = "Error text";

            string xml =
                "<select id='id' name='id'>" +
                    "<option value='value1'>text1</option>" +
                    "<optgroup label='label' >" +
                        "<option value='value2.1'>text2.1</option>" +
                        "<option value='value2.2'>text2.2</option>" +
                    "</optgroup>" +
                    "<option value='value3'>text3</option>" +
                "</select>";
            Select element = xml.Deserialize<Select>();

            StandardSelectValidator obj = new StandardSelectValidator(new Label())
            {
                ErrorText = error
            };

            Assert.False(obj.Validate(element));

            Assert.Equal<int>(1, obj.Errors.Count);
            Assert.Equal<string>(error, obj.Errors[0].Description);
        }
    }
}